package org.intellij.doctree.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import org.intellij.doctree.DocTree;
import org.intellij.doctree.DocTreeComponent;
import org.intellij.doctree.FolderDialog;
import org.intellij.doctree.node.DocTreeNode;
import org.intellij.doctree.node.Folder;
import org.intellij.doctree.node.FolderNode;

public class AddFolder extends TreeEditAction {

    public void actionPerformed(AnActionEvent e) {

        DocTreeComponent component = getComponent(e);
        if (component == null) return;

        DocTree tree = getTree(e);
        if (tree == null) return;

        DocTreeNode node = tree.getCurrentNode();
        if (!(node instanceof FolderNode)) return;

        FolderDialog dialog = new FolderDialog(tree);
        dialog.centerRelativeToParent();
        dialog.show();

        if (dialog.isOkFlag()) {
            Folder newFolder = dialog.getFolder();
            component.addFolder((FolderNode) node, newFolder);
        }

    }

    public void update(AnActionEvent e) {
        boolean actionEnabled = false;
        DocTree tree = getTree(e);
        if (tree != null) {
            DocTreeNode node = tree.getCurrentNode();
            if (node != null && (node instanceof FolderNode)) actionEnabled = true;
        }
        e.getPresentation().setEnabled(actionEnabled);
    }

}
